//
//  LZQ_NetworkTool.swift
//  SwiftExercise
//
//  Created by King on 2021/6/18.
//

import UIKit
import SwiftyJSON
enum SmsCodeType {
    case register
    case forgetPass
}

class LZQ_NetworkTool: NSObject {
    
    static let manager = LZQ_NetworkTool()
    
    struct result {
        var succ:Bool = false
        var message:String?
        var data:[String:Any]?
        var code:Int?
    }
    //本地参数
    var localPara:[String:String] = ["deviceType":""]
    
    func handleResponse(JSON json:JSON) -> result {
//        if LZQ_Utils.isBlankString(num: json["res_msg"].string) == "" {
//            SVProgressHUD.showError(withStatus: "数据错误")
//        }
        return result(succ: true, message: json["message"].string, data: json["data"].dictionary, code: json["code"].int)
    }
    
    func loadData(url:String,para:[String:String],isShowLoading:Bool = false,competion:@escaping((result?) -> Void)) {
        for (key,val) in para {
            localPara[key] = val
        }
        
        if isShowLoading {
            Toast.showActivity()
        }
        

        
        let urlStr = url + kBase_url
        
        LZQ_Network.requestWith(Method: .post, URL: urlStr, Parameter: localPara, Token: nil) { (res) in
            do{
                let json = JSON(res.data!)
                if JSON.null != json{
                    let aResult = self.handleResponse(JSON:json)
                    if aResult.code == 0 {
                        competion(aResult)
                    }else{
                        competion(nil)
                        Toast.showToast(msg: aResult.message ?? "")
                    }
                    Toast.hiddenActivity()
                }
            }
        }
    }
}
